package cz.destil.settleup.gui;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.Spinner;
import com.py.korona.R;

public class MultiSpinner extends Spinner {
    public interface MultiSpinnerListener {
        /**
         * Fired, when user has selected muti spinner item
         * @param selected
         */
        public void onItemsHadSelected(Map<Serializable, MultiChoiceItem> selected);
    }
    private MultiChoiceArrayAdapter adapter_;
    private MultiSpinnerListener    listener;

    public MultiSpinner(Context context) {
        super(context);
    }

    public MultiSpinner(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

    public MultiSpinner(Context context, AttributeSet attrs, int defStyle, int mode) {
        super(context, attrs, defStyle, mode);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean performClick() {
        int count = adapter_.getCount();
        String[] texts = new String[count];
        boolean[] checks = new boolean[count];
        for (int i = 0; i < count; i++) {
            MultiChoiceItem item = adapter_.getItem(i);
            texts[i] = item.text();
            checks[i] = item.isChecked();
        }
        /*
         * 
         * init dialog
         */
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
        dialog.setMultiChoiceItems(texts, checks, new OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int position, boolean isChecked) {
                adapter_.setItemAtIndexChecked(position, isChecked);
            }
        });
        dialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                listener.onItemsHadSelected(adapter_.getChangedItems());
            }
        });
        dialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        dialog.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.setTitle(R.string.title_alert_category_check);
        dialog.show();
        return true;
    }

    public void setItems(List<MultiChoiceItem> items, String hint, MultiSpinnerListener listener) {
        this.listener = listener;
        if (null == adapter_) {
            adapter_ = new MultiChoiceArrayAdapter(getContext(), android.R.layout.simple_spinner_item, android.R.id.text1);
        }
        adapter_.setHint(hint);
        adapter_.clear();
        if (null != items) {
            adapter_.addAll(items);
        }
        setAdapter(adapter_);
    }
}
